<template>
  <el-dialog :visible="visible" :title="formTitle" width="40%" :before-close="close">
    <el-form :model="localDbConnectionConfig" :rules="rules" ref="form">
      <el-form-item label="名称" prop="name">
        <el-input v-model="localDbConnectionConfig.name"></el-input>
      </el-form-item>
      <el-form-item label="数据库类型" prop="dbType">
        <el-select v-model="localDbConnectionConfig.dbType" placeholder="请选择">
          <el-option label="MySQL" value="MySQL"></el-option>
          <el-option label="PostgreSQL" value="PostgreSQL"></el-option>
<!--          <el-option label="Oracle" value="Oracle"></el-option>-->
<!--          <el-option label="SQL Server" value="SQL Server"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item label="主机" prop="host">
        <el-input v-model="localDbConnectionConfig.host"></el-input>
      </el-form-item>
      <el-form-item label="端口" prop="port">
        <el-input v-model="localDbConnectionConfig.port"></el-input>
      </el-form-item>
      <el-form-item label="数据库名称" prop="dbName">
        <el-input v-model="localDbConnectionConfig.dbName"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="localDbConnectionConfig.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="localDbConnectionConfig.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="Schema" prop="schemaName">
        <el-input v-model="localDbConnectionConfig.schemaName"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-switch v-model="localDbConnectionConfig.enabled"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  props: {
    dbConnectionConfig: {
      type: Object,
      default: () => ({})
    },
    visible: {
      type: Boolean
    }
  },
  data() {
    return {
      localDbConnectionConfig: { ...this.dbConnectionConfig },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        dbType: [{ required: true, message: '数据库类型不能为空', trigger: 'change' }],
        host: [{ required: true, message: '主机不能为空', trigger: 'blur' }],
        port: [{ required: true, message: '端口不能为空', trigger: 'blur' }],
        dbName: [{ required: true, message: '数据库名称不能为空', trigger: 'blur' }],
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    };
  },
  watch: {
    dbConnectionConfig: {
      deep: true,
      handler(newVal) {
        this.localDbConnectionConfig = { ...newVal };
      }
    }
  },
  computed: {
    formTitle() {
      return this.localDbConnectionConfig.id ? '编辑数据库连接配置' : '新增数据库连接配置';
    }
  },
  methods: {
    close() {
      this.localDbConnectionConfig = {};
      this.$refs.form.clearValidate();
      this.$emit('close');
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('save', this.localDbConnectionConfig);
        }
      });
    }
  }
};
</script>